import router from './router'
import {checkAuth} from "@/utils/checkAuth"
import { RouteLocationNormalized } from 'vue-router';

router.beforeEach(async (to:RouteLocationNormalized, from, next) => {
  if (window.localStorage.getItem('token')) {//有token
    if (to.path === '/login') {// 是否是去登录页
      next();
      return;
    }else{
      if(to.meta.requiresAuth){//需要权限
        let isAuth = checkAuth(to.meta.requiresAuth as String);// 判断当前用户是否有权限--权限信息储存在缓存中
        if(isAuth){
          next();
        }else{
          next({
            path:'/index'
          })
        }
        return;
      }
      next();
      return;
    }
  }else {//没有token
    if (to.path === '/login') {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})
